"""
__project__ = 'thc'
__file_name__ = 'Excelrun'
__author__ = 'thc'
__time__ = '2021/1/4/004 17:00'
__product_name = PyCharm
-*- coding:utf-8 -*-
"""

import ddt
import logging
import unittest, time
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import traceback
from Exceldata import ExcelData


logging_1 = '%(asctime)s_%(name)s_%(levelname)s_%(message)s'
logging_2 = '%Y-%m-%d %H:%M:%S'
logging.basicConfig(
    level=logging.INFO,
    format=logging_1,
    datefmt=logging_2,
    filename='log.log',
    filemode='w'
)

excelpath = r'./测试数据.xlsx'
sheetName = u'搜索数据表'
excel = ExcelData(excelpath, sheetName)

@ddt.ddt
class Ddt_1(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get("https://www.baidu.com/")

    @ddt.data(*excel.getDatasFromSheet())
    def test_1(self, data):
        testdata, expectdata = tuple(data)
        try:
            driver = self.driver
            driver.find_element_by_id('kw').clear()
            driver.find_element_by_id('kw').send_keys(testdata)
            driver.find_element_by_id('su').click()
            time.sleep(3)
            self.assertTrue(expectdata in driver.page_source)
        except NoSuchElementException as e:
            logging.error('没有找到该元素，异常信息是：'+traceback.format_exc())
        except AssertionError as e:
            logging.error('搜索—%s,预期-%s,失败')
        except Exception as e:
            logging.error('异常错误，错误信息是：'+traceback.format_exc())
        else:
            logging.info('搜索-%s,预期-%s,成功')

    def tearDown(self):
        self.driver.quit()


if __name__ == '__main__':
    unittest.main()